package com.heima.service.impl;

import ch.qos.logback.core.testUtil.RandomUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.dto.RoleDto;
import com.heima.mapper.RoleMapper;
import com.heima.pojo.Role;
import com.heima.pojo.Roles;
import com.heima.service.RoleService;
import com.heima.vo.PageResult;
import com.heima.vo.RoleVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;

@Service
@Slf4j
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    /**
     * 角色列表查询
     * @param roleDto
     * @return
     */
    @Override
    public PageResult list(RoleDto roleDto) {
        PageHelper.startPage(roleDto.getPageNum(),roleDto.getPageSize());
        Page<RoleVo> page = roleMapper.list(roleDto);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRows(page.getResult());
        return pageResult;
    }

    /**
     * 批量删除角色
     * @param ids
     */
    @Override
    @Transactional
    public void delete(List<Integer> ids) {
        if(ids == null){
            throw new RuntimeException("参数异常");
        }
        for(Integer id : ids){
            roleMapper.deleteById(id);
        }
    }

    /**
     * 根据角色编号查询角色信息
     * @param roleId
     */
    @Override
    public RoleVo getById(Integer roleId) {
        return roleMapper.getById(roleId);
    }

    /**
     * 修改角色信息
     * @param role
     */
    @Override
    public void updateRole(Role role) {
        roleMapper.updateRole(role);
    }

    /**
     * 根据角色id修改数据权限
     * @param dataScope
     * @param roleId
     */
    @Override
    public void updateDataScope(String dataScope, Integer roleId) {
        roleMapper.updateDataScope(dataScope,roleId);
    }

    /**
     * 新增角色
     * @param roleVo
     */
    @Override
    public void addRole(RoleVo roleVo) {
        Role role = new Role();
        //生成三位数随机整数
        Integer random = new Random().nextInt(999)+100 ;
        //判断生成的随机数不等于已有的编号，等于时重新生成直到不等于
        //死循环
        while (true){
            if(roleMapper.selectCountById(random) == 0){
                break;
            }else{
                random = new Random().nextInt(999)+100 ;
                log.info("生成的随机数为：{}",random);
            }
        }
        role.setRoleId(random);
        role.setRoleName(roleVo.getRoleName());
        role.setRoleKey(roleVo.getRoleKey());
        role.setRoleSort(roleVo.getRoleSort());
        role.setStatus(roleVo.getStatus());
        role.setCreateTime(LocalDateTime.now());
        role.setDataScope(roleVo.getDataScope());
        roleMapper.addRole(role);
    }

    /**
     * 修改角色状态
     * @param role
     */
    @Override
    public void updateStatus(Role role) {
        //查询角色状态，如果状态为正常则修改为停用，反之为正常
        String status = role.getStatus();
        if("0".equals(status)){
            role.setStatus("1");
        }else{
            role.setStatus("0");
        }
        roleMapper.updateCourse(role);
    }


}
